package com.tianji.learning.service.impl;

import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.ReplyDTO;
import com.tianji.learning.domain.po.InteractionQuestion;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.mapper.InteractionReplyMapper;
import com.tianji.learning.service.IInteractionQuestionService;
import com.tianji.learning.service.IInteractionReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 互动问题的回答或评论 服务实现类
 * </p>
 *
 * @author dayu
 * @since 2024-08-25
 */
@Service
@RequiredArgsConstructor
public class InteractionReplyServiceImpl extends ServiceImpl<InteractionReplyMapper, InteractionReply> implements IInteractionReplyService {
  private final IInteractionQuestionService questionService;

  @Override
  public void saveReply(ReplyDTO reply) {
    InteractionReply interactionReply = BeanUtils.copyProperties(reply, InteractionReply.class);
    interactionReply.setUserId(UserContext.getUser());
    save(interactionReply);
    questionService.lambdaUpdate()
            .eq(InteractionQuestion::getId, reply.getQuestionId())
            .set(InteractionQuestion::getLatestAnswerId,interactionReply.getId())
            .update();
  }
}
